//
//  VCOfTestResult.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/8.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfTestResult.h"
#import "BaseTableView.h"
#import "MJMacro.h"
#import "TVCellOfTestResult.h"
#import "ZWYNetTool.h"
#import "DataOfTestResult.h"
#import "RootClassOfTestResult.h"
#import "Similar_UserOfTestResult.h"
#import "RootClassOfBeginDoTest.h"
#import "DataOfBeginDoTest.h"
#import "UserOfDataBeginDoTest.h"
#import "TVCellOfBeginDoTest.h"
#import "UIImageView+WebCache.h"
@interface VCOfTestResult ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *testResultTableView;
@property (nonatomic, strong) RootClassOfTestResult *rootClassOfTestResult;
@end

@implementation VCOfTestResult

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 数据处理
    [self handleData];
    // 创建 tableView
    [self createTableView];
}
#pragma mark - 数据处理
- (void)handleData{
    NSString *stringOfPostBody = [NSString stringWithFormat:@"id=%@&choice_ids=%@&token=0d4eeb8732f9ae2c73401002570ceb93&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&sign=b85f2ecc1540c1af0bb7a985bb607e59&_version=4.0", _myID, _stringOfhoice_ids];
    [ZWYNetTool POST:@"http://yiapi.xinli001.com/yi/post-test-result.json/" andBody:stringOfPostBody andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type" : @"application/x-www-form-urlencoded"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _rootClassOfTestResult = [RootClassOfTestResult modelDic:result];
        [_testResultTableView reloadData];
        NSLog(@"%@", result);
    } andFailureBlock:^(NSError *error) {

    }];
}
#pragma mark - 创建 tableView
- (void)createTableView{
    _testResultTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _testResultTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_testResultTableView];
    _testResultTableView.delegate = self;
    _testResultTableView.dataSource = self;
    [_testResultTableView registerNib:[UINib nibWithNibName:@"TVCellOfTestResult" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfTestResult"];
    [_testResultTableView registerNib:[UINib nibWithNibName:@"TVCellOfBeginDoTest" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TVCellOfBeginDoTest"];
}
#pragma mark - 返回分区🌲
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
#pragma mark - tableView 代理方法 Row 数量的确定
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    } else {
//        return _rootClassOfTestResult.data.count;
//        return 2;
        NSLog(@"------------------%ld", _rootClassOfBeginDoTest.data.count);
        return _rootClassOfBeginDoTest.data.count;
        return 9;
    }
}
#pragma mark - tableView 代理方法 cell 的赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        TVCellOfTestResult *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfTestResult" forIndexPath:indexPath];
        cell.titleLabel.text = _rootClassOfTestResult.data.title;
        cell.contentLabel.text = _rootClassOfTestResult.data.content;
        return cell;
    } else {
        TVCellOfBeginDoTest *cell = [tableView dequeueReusableCellWithIdentifier:@"TVCellOfBeginDoTest" forIndexPath:indexPath];
        [cell.avatarImageView sd_setImageWithURL:[NSURL URLWithString:_rootClassOfBeginDoTest.data[indexPath.row].user.avatar] placeholderImage:[UIImage imageNamed:@"JustAMoment"]];
        cell.avatarImageView.layer.cornerRadius = 20;
        cell.avatarImageView.clipsToBounds = YES;
        cell.nicknameLabel.text = _rootClassOfBeginDoTest.data[indexPath.row].user.nickname;
        cell.createdLabel.text = _rootClassOfBeginDoTest.data[indexPath.row].created;
        cell.contentLabel.text = _rootClassOfBeginDoTest.data[indexPath.row].content;

        return cell;
    }
}
#pragma mark - tableView 的代理方法 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        // 180 是我在 xib 里面控件的高度和之间的间距之和
        return 200 + [self selfSizeHeight];
    } else {
        return SCREEN_HEIGHT * 0.2;
    }
}
#pragma mark - 自适应高度
- (CGFloat)selfSizeHeight{
    int labelFontSize = 17;
    if (IS_IPHONE_5) {
        labelFontSize = 14;
    }
    CGRect newRect = [_rootClassOfTestResult.data.content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 95, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:labelFontSize]} context:nil];
    return newRect.size.height;
}
#pragma mark - 其他, 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
